package LeetcodeTest_tewnty_one;
//47. 礼物的最大价值

public class maxValue
{
    public static void main(String[] args)
    {
        int[][] grid={{1,3,1},{1,5,1},{4,2,1}};
        System.out.println(Solution.maxValue(grid));
    }
}
class Solution
{
    public static int maxValue(int[][] grid)
    {
        int i,j,m,n;
        i=j=0;
        m=grid.length;
        n=grid[0].length;
        int[][] maxGrid=new int[m][n];
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i==0 && j==0)
                    maxGrid[i][j]=grid[i][j];
                else if(i==0)
                {
                    maxGrid[i][j]=grid[i][j]+maxGrid[i][j-1];
                }
                else if(j==0)
                    maxGrid[i][j]=grid[i][j]+maxGrid[i-1][j];
                else
                    maxGrid[i][j]=grid[i][j]+Math.max(maxGrid[i][j-1],maxGrid[i-1][j]);
            }
        }
        return maxGrid[m-1][n-1];
    }
}